package com.iflytek.uoamp.notify.mail.format;

import java.util.List;

import com.iflytek.uoamp.common.notify.obj.Content;
import com.iflytek.uoamp.common.notify.obj.Notification;
import com.iflytek.uoamp.common.notify.util.NotifyUtils;

public class FormatCommonMail extends FixedMailFormatter {
	
    private static final String COMMONTEMPLATE = getTemplate("common_template.html");
    private static final String TITLESTART = "<!-- TITLE START -->";
    private static final String TITLEEND = "<!-- TITLE END -->";
    private static final String TABLESTART = "<!-- TABLE START -->";
    private static final String TABLEEND = "<!-- TABLE END -->";
    private static final String HEADERSTART = "<!-- HEADER START -->";
    private static final String HEADEREND = "<!-- HEADER END -->";
    private static final String ROWSTART = "<!-- ROW START -->";
    private static final String ROWEND = "<!-- ROW END -->";

    @Override
    public String pretty(Notification notification, List<?> contents) {
        StringBuilder content = new StringBuilder(getTitle(contents) + "@@" + COMMONTEMPLATE);
        StringBuilder data = new StringBuilder();
        
        for(Object item : contents) {
            String table = fillTables((Content)item);
            data.append(table);
        }
        fillAll(content, data.toString(), TABLESTART, TABLEEND);

        //wlhu2, 添加组件联系人信息
        fillPrincipalData(content, notification);
        return content.toString();
    }
    
    public String fillTables(Content content) {
        List<String> tables = content.getQuota();
        String output = content.getOutput();
        StringBuilder total = new StringBuilder();
        String title = null;
        String header = null;
        String data = null;
        
        if(tables == null) {
            return "";
        }
        
        for(String item : tables) {
            String tableString = NotifyUtils.getAttributeValue(output, item);
            StringBuilder table = new StringBuilder(getTableTemplate(COMMONTEMPLATE));
            
            title = fillTitle(item);
            fillAll(table, title, TITLESTART, TITLEEND);
            
            int pos = tableString.indexOf("&");
            if(pos > 0 && pos != tableString.length() - 1) {	//不存在&符号或者是&符号前、后没有数据
                header = fillHeader(tableString.substring(0, pos));
                data = fillAllData(tableString.substring(pos + 1));
                
                fillAll(table, header, HEADERSTART, HEADEREND);
                fillAll(table, data, ROWSTART, ROWEND);
            }
            
            total.append(table);
            total.append("<br />");
        }
        
        return total.toString();
    }
    
    private String fillTitle(String title) {
        StringBuilder content = new StringBuilder(getTitleTemplate(COMMONTEMPLATE));
        replace(content, "${key}", title);
        
        return content.toString();
    }
    
    private String fillHeader(String headers) {
        String[] items = headers.split("；");
        StringBuilder blocks = new StringBuilder();
        for(String item : items) {
            StringBuilder header = new StringBuilder(getHeaderTemplate(COMMONTEMPLATE));
            replace(header, "${column}", item);
            blocks.append(header);
        }
        
        return blocks.toString();
    }
    
    private String fillAllData(String dataList) {
        String[] datas = dataList.split("&");
        StringBuilder blocks = new StringBuilder();
        
        for(String data : datas) {
            StringBuilder tableRow = new StringBuilder(getTableRow(COMMONTEMPLATE));
            fillRowData(tableRow, fillData(data));
            blocks.append(tableRow);
        }
        
        return blocks.toString();
    }
    
    private String fillData(String values) {
        String[] items = values.split("；");
        StringBuilder rowData = new StringBuilder();
        for(String item : items) {
            StringBuilder column = new StringBuilder(getDataTemplate(COMMONTEMPLATE));
            replace(column, "${value}", item);
            rowData.append(column);
        }
        
        return rowData.toString();
    }
    
    private String getTableTemplate(String template) {
        return getBlock(template, TABLESTART, TABLEEND);
    }
    
    private String getTitleTemplate(String template) {
        return getBlock(template, TITLESTART, TITLEEND);
    }
    
    private String getHeaderTemplate(String template) {
        return getBlock(template, HEADERSTART, HEADEREND);
    }
    
    private String getTableRow(String template) {
        return getBlock(template, ROWSTART, ROWEND);
    }
    
    private void fillRowData(StringBuilder rowTemplate, String rowData) {
        fillAll(rowTemplate, rowData, DATASTART, DATAEND);
    }

}
